<template>
  <page-header-wrapper>
    <a-card :bordered="false" class="card_top_padding" style="margin-bottom: 11px">
      <div class="searchBox">
        <div class="select">
          <span>提现方式：</span>
          <a-select style="width: 220px" v-model="queryParam.type">
            <a-select-option :value="item.value" v-for="(item, index) in types" :key="index">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </div>
        <span style="margin-top: 14px; margin-right: 10px">起止时间:</span>
        <a-range-picker
          style="margin-top: 14px"
          :show-time="{ format: 'HH:mm' }"
          format="YYYY-MM-DD HH:mm"
          valueFormat="YYYY-MM-DD HH:mm"
          :placeholder="['开始时间', '结束时间']"
          v-model="queryTime"
          @change="changeTime"
        />
        <a-button type="primary" @click="getWithdrawList" style="margin-left: 10px">查询</a-button>
        <a-button :loading="exporting" @click="exportWL" type="primary" style="margin-left: 9px">导出Excel</a-button>
      </div>
    </a-card>
    <a-card :bordered="false" class="card_style_re">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getWithdrawList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="1"> 已审核 </a-radio-button>
          <a-radio-button value="0"> 未审核 </a-radio-button>
          <a-radio-button value="3"> 已拒绝 </a-radio-button>
          <a-radio-button value="2"> 已打款 </a-radio-button>
        </a-radio-group>
        <div>
          <a-button type="primary" @click="showWithdrow = true">+ 申请提现</a-button>
          <!-- <a-button>导出报表</a-button> -->
        </div>
      </div>
      <a-divider />
      <a-table
        :columns="columns"
        :data-source="withDrawList"
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
      >
        <span slot="type" slot-scope="text">
          {{ text == 0 ? '微信转账' : text == 1 ? '支付宝转账' : '银行卡转账' }}
        </span>
        <span slot="price" slot-scope="text"> ￥{{ text }} </span>
        <span slot="created_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span slot="updated_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <div slot="status" slot-scope="scope">
          <div class="status">
            <span
              :class="scope == 0 ? 'waiting' : scope == 1 ? 'close' : scope == 2 ? 'open' : scope == 3 ? 'refuse' : ''"
            ></span>
            <span>{{
              scope == 0 ? '未处理' : scope == 1 ? '待打款' : scope == 2 ? '已完成' : scope == 3 ? '已拒绝' : ''
            }}</span>
          </div>
        </div>
      </a-table>
    </a-card>
    <!-- 提现弹窗 -->
    <a-modal v-model="showWithdrow" title="提现申请" @ok="confirmWithdrow" @cancel="closeWithdraw">
      <a-form-model
        :model="withdrowForm"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 14 }"
        ref="withdrawForm"
        :rules="rules"
      >
        <a-form-model-item label="转账方式">
          <a-radio-group v-model="withdrowForm.type">
            <a-radio :value="0"> 微信转账 </a-radio>
            <a-radio :value="1"> 支付宝转账 </a-radio>
            <a-radio :value="2"> 银行卡转账 </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="微信号" prop="account" v-if="withdrowForm.type == 0">
          <a-input v-model="withdrowForm.account" placeholder="请输入微信号" />
        </a-form-model-item>
        <a-form-model-item label="微信昵称" prop="name" v-if="withdrowForm.type == 0">
          <a-input v-model="withdrowForm.name" placeholder="请输入备注昵称" />
        </a-form-model-item>
        <a-form-model-item label="支付宝账号" prop="account" v-if="withdrowForm.type == 1">
          <a-input v-model="withdrowForm.account" placeholder="请输入支付宝账号" />
        </a-form-model-item>
        <a-form-model-item label="支付宝真实姓名" prop="name" v-if="withdrowForm.type == 1">
          <a-input v-model="withdrowForm.name" placeholder="请输入支付宝真实姓名" />
          <span class="tip">*请填写真实姓名，否则将不予转账</span>
        </a-form-model-item>
        <a-form-model-item label="银行卡账号" prop="account" v-if="withdrowForm.type == 2">
          <a-input v-model="withdrowForm.account" placeholder="请输入银行卡账号" />
        </a-form-model-item>
        <a-form-model-item label="开户行" prop="bank" v-if="withdrowForm.type == 2">
          <a-input v-model="withdrowForm.bank" placeholder="请输入开户行" />
        </a-form-model-item>
        <a-form-model-item label="开户人" prop="name" v-if="withdrowForm.type == 2">
          <a-input v-model="withdrowForm.name" placeholder="请输入开户人" />
        </a-form-model-item>
        <a-form-model-item label="提现金额" prop="cash">
          <a-input v-model="withdrowForm.cash" placeholder="请输入提现金额" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { allShop, withDrawList, expwithDrawList, applyWithdraw } from '@/api/outlet'
import { STable } from '@/components'
const columns = [
  {
    title: '提现方式',
    dataIndex: 'type',
    scopedSlots: {
      customRender: 'type',
    },
  },
  {
    title: '提现金额',
    dataIndex: 'price',
    scopedSlots: {
      customRender: 'price',
    },
    sorter: (a, b) => a.price - b.price,
  },
  {
    title: '申请时间',
    dataIndex: 'created_at',
    sorter: (a, b) => a.created_at - b.created_at,
    scopedSlots: {
      customRender: 'created_at',
    },
  },
  {
    title: '审批时间',
    dataIndex: 'updated_at',
    sorter: (a, b) => a.updated_at - b.updated_at,
    scopedSlots: {
      customRender: 'updated_at',
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status',
    },
  },
]

export default {
  name: 'StaffList',
  components: {
    STable,
  },
  data() {
    let checkBank = (rule, value, callback) => {
      if (this.withdrowForm.type == 2) {
        if (!this.withdrowForm.bank || this.withdrowForm.bank == '') {
          callback(new Error('请输入开户行'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      exporting: false,
      // 表头
      columns,
      // 查询条件参数
      queryParam: {
        status: '-1',
        pageNp: 1,
        pageSize: 10,
        type: null,
        start_time: null,
        end_time: null,
      },
      // 提现记录列表
      withDrawList: [],
      // 数据总条数
      totalCount: 0,
      // 起止时间
      queryTime: [],
      // 所有门店
      allShop: [],
      // 所有提现方式
      types: [
        {
          value: 0,
          name: '微信',
        },
        {
          value: 1,
          name: '支付宝',
        },
        {
          value: 2,
          name: '银行卡',
        },
      ],
      // 是否显示提现弹窗
      showWithdrow: false,
      // 提现表单
      withdrowForm: {
        // 转账方式 0微信 1支付宝 2银行
        type: 0,
        // 微信号 银行卡账号 微信号
        account: '',
        // 微信昵称 开户人 支付宝真实姓名
        name: '',
        // 开户行
        bank: '',
        // 提现金额
        cash: '',
      },
      // 提现表单验证规则
      rules: {
        account: [
          {
            required: true,
            message: '请输入账号',
            trigger: 'blur',
          },
        ],
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur',
          },
        ],
        cash: [
          {
            required: true,
            message: '请输入提现金额',
            trigger: 'blur',
          },
        ],
        bank: [
          {
            validator: checkBank,
            trigger: 'change',
          },
        ],
      },
    }
  },
  created() {
    this.getWithdrawList()
  },
  methods: {
    exportWL(e) {
      this.exporting = true;
      let parameter = {...this.queryParam};
      Object.assign(parameter, {
        pageNo: 1,
        pageSize: 100000,
        _filename: '提现明细.xlsx',
        export: 1
      })
      expwithDrawList(parameter).then(()=>{
        this.exporting = false;
      });
    },
    // 重置查询参数
    resetQueryParams() {
      Object.assign(this.queryParam, {
        status: '-1',
        pageNp: 1,
        pageSize: 10,
        type: null,
        start_time: null,
        end_time: null,
      })
      this.getWithdrawList()
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getWithdrawList()
    },
    // 获取提现申请列表
    async getWithdrawList() {
      const hide = this.$message.loading('加载中', 0)
      const res = await withDrawList(this.queryParam)
      if (res.code == 0) {
        console.log(res)
        this.withDrawList = res.data.data
        this.totalCount = res.data.totalCount
      }
      hide()
    },
    // 切换筛选时间
    changeTime(e) {
      if (e.length) {
        this.queryParam.start_time = e[0]
        this.queryParam.end_time = e[1]
      } else {
        this.queryParam.start_time = null
        this.queryParam.end_time = null
      }
    },
    // 点击确认提现
    confirmWithdrow() {
      this.$refs['withdrawForm'].validate((valid) => {
        if (valid) {
          this.applyWithdraw()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 关闭提现弹窗
    closeWithdraw() {
      this.withdrowForm.type = 0
      this.withdrowForm.account = ''
      this.withdrowForm.name = ''
      this.withdrowForm.bank = ''
      this.withdrowForm.cash = ''
    },
    // 申请提现
    async applyWithdraw() {
        const res = await applyWithdraw(this.withdrowForm)
        console.log(res);
        if(res.code == 0) {
            this.getWithdrawList()
            this.closeWithdraw()
            this.showWithdrow = false
        }
    }
  },
}
</script>

<style lang="less" scoped>
.permission-form {
  /deep/ .permission-group {
    margin-top: 0;
    margin-bottom: 0;
  }
}
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-top: 14px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-top: 14px;
    margin-right: 20px;
  }
  .ant-btn {
    margin-top: 14px;
  }
}
.action {
  display: flex;
  align-items: center;
  .line {
    width: 1px;
    height: 14px;
    background-color: #d8d8d8;
  }
}
.name {
  display: flex;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}

.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 8px;
  }
  .close {
    background-color: #c6c6c6;
  }
  .open {
    background-color: #65c817;
  }
  .refuse {
    background-color: #f5222d;
  }
  .waiting {
    background-color: #faad14;
  }
}
</style>
